package com.ch.mobilesha.tools;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class ViewTools {

	private static String OK="确定";
	private static String CANCEL="取消";
	private static String PROMPT ="提示信息";
	/**
	 * 显示带标题的alert提示信息
	 * @param context
	 * @param title
	 * @param message
	 */
	public static void showMessage(Context context,String title,String message){
		AlertDialog.Builder builders = new AlertDialog.Builder(context);
		if(title!=null){
			builders.setTitle(title);
		}else{
			builders.setTitle(PROMPT);
		}
		builders.setMessage(message);
		builders.setPositiveButton(OK, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builders.show();
	}
	
	/**
	 * 显示带标题的选择提示信息
	 * @param context
	 * @param title
	 * @param message
	 */
	public static void showSelect(Context context,String title,String[] items,OnClickListener listener){
		AlertDialog.Builder builders = new AlertDialog.Builder(context);
		if(title!=null){
			builders.setTitle(title);
		}else{
			builders.setTitle(PROMPT);
		}
		builders.setItems(items, listener);
		builders.show();
	}
	/**
	 *  显示不带标题的alert提示信息
	 * @param context
	 * @param message
	 */
	public static void showMessage(Context context,String message){
		AlertDialog.Builder builders = new AlertDialog.Builder(context);
		builders.setTitle(PROMPT);
		builders.setMessage(message);
		builders.setPositiveButton(OK, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builders.show();
	}
	
	/**
	 * 显示带标题，确认按钮的alert提示信息
	 * @param context
	 * @param title
	 * @param message
	 * @param btnText
	 * @param listener
	 */
	public static void showMessage(Context context,String title,String message,String btnText,DialogInterface.OnClickListener listener){
		AlertDialog.Builder builders = new AlertDialog.Builder(context);
		if(title!=null){
			builders.setTitle(title);
		}else{
			builders.setTitle(PROMPT);
		}
		builders.setMessage(message);
		builders.setPositiveButton(btnText,listener);
		builders.show();
	}
	
	/**
	 * 显示带标题，确认，取消按钮的alert提示信息
	 * @param context
	 * @param title
	 * @param message
	 * @param btnText
	 * @param listener
	 */
	public static void showConfirm(Context context,String title,String message,String okText,String cancelText,DialogInterface.OnClickListener okListener,DialogInterface.OnClickListener cancelListener){
		AlertDialog.Builder builders = new AlertDialog.Builder(context);
		if(title!=null){
			builders.setTitle(title);
		}else{
			builders.setTitle(PROMPT);
		}
		if(cancelText==null){
			cancelText = CANCEL;
		}
		builders.setMessage(message);
		builders.setPositiveButton(okText,okListener);
		builders.setNegativeButton(cancelText, cancelListener);
		builders.show();
	}
	
	/**
	 * 显示带标题，确认按钮的alert提示信息
	 * @param context
	 * @param title
	 * @param message
	 * @param btnText
	 * @param listener
	 */
	public static void showMessage(Context context,String title,String message,String okText ,DialogInterface.OnClickListener okListener,OnKeyListener onKeyListener){
		AlertDialog.Builder builders = new AlertDialog.Builder(context);
		if(title!=null){
			builders.setTitle(title);
		}else{
			builders.setTitle(PROMPT);
		}
		builders.setMessage(message);
		builders.setPositiveButton(okText,okListener);
		builders.setOnKeyListener(onKeyListener);
		builders.show();
	}
	
	/**
	 * 获取一个分割线
	 */
	public static TextView getDivider(Context view , int height){
		TextView tvDivider =new TextView(view);
		LayoutParams paramsDivider = new LayoutParams(LayoutParams.FILL_PARENT,height);
		tvDivider.setLayoutParams(paramsDivider);
		tvDivider.setBackgroundResource(android.R.drawable.divider_horizontal_dark);
		return tvDivider;
	}
	/**
	 * 显示短时间的toast
	 * @param context
	 * @param message
	 */
	public static void showShortToast(Context context,String message){
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}
	
	/**
	 * 显示长时间的toast
	 * @param context
	 * @param message
	 */
	public static void showLongToast(Context context,String message){
		Toast.makeText(context, message, Toast.LENGTH_LONG).show();
	}
	
	/**
	 * 显示正在加载提示信息
	 * @param context
	 * @param message
	 */
	public static ProgressDialog showLoading(Context context,String title,String message){
		return ProgressDialog.show(context, title, message,true,true);
	}
	/**
	 * 显示正在加载提示信息
	 * @param context
	 * @param message
	 */
	public static ProgressDialog showLoading(Context context,String message){
		return ProgressDialog.show(context,null, message,true,true);
	}
	/**
	 * 显示分享信息
	 * @param context
	 * @param message
	 */
	public static void showShare(Context context,String content){
		Intent intent =new Intent(Intent.ACTION_SEND);
		intent.setType("image/*");
		intent.putExtra(Intent.EXTRA_TEXT, content);
		intent.putExtra(Intent.EXTRA_SUBJECT, "故事梦分享");
		context.startActivity(intent);
	}
	
	/**
	 * 隐藏弹出的对话框
	 */
	public static void hideAlertDialog(AlertDialog alertDialog){
		if(alertDialog!=null && alertDialog.isShowing()){
			alertDialog.dismiss();
			alertDialog=null;
		}
	}
	
}
